package com.jprocms.module.cms.config;

import com.jpro.framework.security.core.LoginUser;
import com.jpro.module.system.api.permission.PermissionApi;
import com.jprocms.module.cms.permission.*;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;

import java.util.List;

/**
 * 基于栏目的数据权限 AutoConfiguration
 *
 * @author 泽睿
 */
@AutoConfiguration
@ConditionalOnClass(LoginUser.class)
@ConditionalOnBean(value = {PermissionApi.class, ChannelPermissionRuleCustomizer.class,
        ContentPermissionRuleCustomizer.class, SitePermissionRuleCustomizer.class})
public class ChannelPermissionAutoConfiguration {

    @Bean
    public ChannelPermissionRule channelPermissionRule(PermissionApi permissionApi,
                                                       List<ChannelPermissionRuleCustomizer> customizers) {
        // 创建 ChannelPermissionRule 对象
        ChannelPermissionRule rule = new ChannelPermissionRule(permissionApi);
        // 补全表配置
        customizers.forEach(customizer -> customizer.customize(rule));
        return rule;
    }

    @Bean
    public ContentPermissionRule contentPermissionRule(PermissionApi permissionApi,
                                                       List<ContentPermissionRuleCustomizer> customizers) {
        // 创建 ContentPermissionRule 对象
        ContentPermissionRule rule = new ContentPermissionRule(permissionApi);
        // 补全表配置
        customizers.forEach(customizer -> customizer.customize(rule));
        return rule;
    }

    @Bean
    public SitePermissionRule sitePermissionRule(PermissionApi permissionApi,
                                                 List<SitePermissionRuleCustomizer> customizers) {
        // 创建 SitePermissionRule 对象
        SitePermissionRule rule = new SitePermissionRule(permissionApi);
        // 补全表配置
        customizers.forEach(customizer -> customizer.customize(rule));
        return rule;
    }

}
